package com.filepublic.demo.server.controller;

import com.filepublic.demo.common.result.Result;
import com.filepublic.demo.pojo.dto.FileSureDto;
import com.filepublic.demo.pojo.vo.FileDownloadVo;
import com.filepublic.demo.pojo.vo.FileSureVo;
import com.filepublic.demo.pojo.vo.ImageVo;
import com.filepublic.demo.server.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@Api(tags = "文件控制器")
@RequestMapping("/file")
public class FileController {

    @Resource
    FileService fileService;

    @ApiOperation(value = "图片上传")
    @PostMapping("/imageUpload")
    public Result<ImageVo> imageUpload(@RequestParam("image") MultipartFile file) throws IOException {
        ImageVo imageVo = fileService.imageUpload(file);
        return Result.success(imageVo);
    }

    /**
     * 大文件上传
     * @param file
     * @param fileIndex
     * @param fileId
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "大文件上传")
    @PostMapping("/upload")
    public Result bigFileUpload(@RequestParam("file") MultipartFile file,
                             @RequestParam("fileIndex") Integer fileIndex,
                             @RequestParam("fileId") String fileId) throws IOException
    {
        fileService.bigFileUpload(file,fileIndex,fileId);
        return Result.success();
    }

    /**
     * 文件确认
     * @return
     */
    @ApiOperation(value = "文件确认")
    @PostMapping("/sure")
    public Result<FileSureVo> bigFileSure(@RequestBody FileSureDto fileSureDto){
        FileSureVo fileSureVo = fileService.bigFileSure(fileSureDto);
        return Result.success(fileSureVo);
    }

    /**
     * 大文件下载
     * @param fileId
     * @param fileIndex
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "文件下载")
    @GetMapping("/download/{fileId}/{fileIndex}")
    public Result<FileDownloadVo> bigFileDownload(@PathVariable("fileId") String fileId,@PathVariable("fileIndex") Integer fileIndex) throws IOException {
        FileDownloadVo fileDownloadVo = fileService.bigFileDownload(fileId, fileIndex);
        return Result.success(fileDownloadVo);
    }
}





























